//
//  YFHomeZeroCell.m
//  QuanJieShopProject
//
//  Created by 杨帆 on 2018/3/29.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFHomeZeroCell.h"
#import "SDImageCache.h"

@implementation YFHomeZeroCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        KMyLog(@"width = %f, height = %f", frame.size.width, frame.size.height);
        [self makeView:frame];
//        self.backgroundColor = [UIColor lightGrayColor];
    }
    return self;
}

- (void)makeView:(CGRect)frame {
    
    CGFloat padding = 30.f/750.f * UISCREEN_WIDTH;
    
    // Top图片     // 55 65
    CGFloat leftImageVieH = frame.size.height * 65.f/150.f;
    CGFloat leftImageViewW = leftImageVieH * 55.0/65.0;;
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, leftImageViewW, leftImageVieH)];
    leftImageView.image = [UIImage imageNamed:@"top"];
    [self addSubview:leftImageView];
    self.leftImageView = leftImageView;
    
    // Top里的文字
    UILabel *topTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    topTitleLabel.center = CGPointMake(leftImageView.center.x, leftImageView.center.y+3);
    if (UISCREEN_WIDTH > 375) {
        topTitleLabel.center = CGPointMake(leftImageView.center.x, leftImageView.center.y+5);
    }
    [self addSubview:topTitleLabel];
    topTitleLabel.font = [UIFont boldSystemFontOfSize:10];
//    topTitleLabel.backgroundColor = [UIColor blueColor];
    topTitleLabel.textAlignment = NSTextAlignmentCenter;
    topTitleLabel.textColor = [UIColor whiteColor];
    self.topTitleLabel = topTitleLabel;
    
    
    // 图片 正方形
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftImageView.frame)+ padding, 0, frame.size.height, frame.size.height)];
    [self addSubview:imgView];
    self.imageView = imgView;
    
    
    
//    CGFloat titlePadding =
    CGFloat titleX = CGRectGetMaxX(imgView.frame) + padding;
    CGFloat titleW = frame.size.width - CGRectGetMaxX(imgView.frame) - padding*2;
    CGFloat titleH = (frame.size.height)/3; //10是文字间距  - 2*10
    
    // 标题
    UILabel *cellTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleX, 0, titleW, titleH)];
    [self addSubview:cellTitleLabel];
//    cellTitleLabel.backgroundColor = [UIColor blueColor];
    cellTitleLabel.font = [UIFont boldSystemFontOfSize:14];
    self.cellTitleLabel = cellTitleLabel;
    
    // 已售图片    // volume  140 35
    CGFloat soldImgH = titleH * 0.8;
    CGFloat top_bottom_padding = (titleH - soldImgH)/2;
    CGFloat soldImgW = soldImgH * 140.0/35.0;
    CGFloat soldImgY = CGRectGetMaxY(cellTitleLabel.frame) + top_bottom_padding;
    CGFloat soldImgX = titleX;
    UIImageView *soldImageView = [[UIImageView alloc] initWithFrame:CGRectMake(soldImgX, soldImgY, soldImgW, soldImgH)];
    soldImageView.image = [UIImage imageNamed:@"volume"];
    [self addSubview:soldImageView];
//    soldImageView.backgroundColor = [UIColor redColor];
    self.soldImageView = soldImageView;
    
    // 已售文字
    CGFloat cellSoldH = soldImgH;
    CGFloat cellSoldX = soldImgX + cellSoldH * 28.0/35.0; // 火焰的宽度 28 35
    CGFloat cellSoldW = soldImgW - cellSoldH * 28.0/35.0;
    CGFloat cellSoldY = soldImgY;
    UILabel *cellSoldLabel = [[UILabel alloc] initWithFrame:CGRectMake(cellSoldX, cellSoldY, cellSoldW, cellSoldH)];
    cellSoldLabel.textColor = [UIColor whiteColor];
    [self addSubview:cellSoldLabel];
    cellSoldLabel.font = [UIFont systemFontOfSize:10];
    self.cellSoldLabel = cellSoldLabel;
    
    // 价格
    UILabel *cellPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleX, CGRectGetMaxY(soldImageView.frame) + top_bottom_padding, 60, titleH)];
    [self addSubview:cellPriceLabel];
//    cellPriceLabel.backgroundColor = [UIColor blueColor];
//    cellPriceLabel.backgroundColor = [UIColor yellowColor];
    cellPriceLabel.font = [UIFont systemFontOfSize:13];
    cellPriceLabel.textColor = YFColor_Red;
    self.cellPriceLabel = cellPriceLabel;
    
    // 原价
    UILabel *cellOriginalPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(cellPriceLabel.frame), cellPriceLabel.frame.origin.y, 100, titleH)];
    // 先不显示
//    [self addSubview:cellOriginalPriceLabel];
//    cellOriginalPriceLabel.backgroundColor = [UIColor blueColor];
    cellOriginalPriceLabel.font = [UIFont systemFontOfSize:12];
    cellOriginalPriceLabel.textColor = [UIColor lightGrayColor];
    self.cellOriginalPriceLabel = cellOriginalPriceLabel;
    
}

// 
- (void)cellUpdateWithModel:(HjrbkModel *)model order:(NSInteger)order {

    UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.image_url];
    
    if (cachedImage) {
        self.imageView.image = cachedImage;
    }else {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL_IMG,model.image_url]] placeholderImage:[UIImage imageNamed:@"noImage"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            
            if (!error) {
                // 保存图片
                [[SDImageCache sharedImageCache] storeImage:image forKey:model.image_url completion:nil];
            }
        }];
    }
    
    
    self.cellTitleLabel.text = [NSString stringWithoutNilFormart:model.lable1];
    self.cellSoldLabel.text = @"库存1000+";
//    @"已售100件";
    self.cellPriceLabel.text = [NSString stringWithFormat:@"￥%@",model.lable2];
    self.topTitleLabel.text = [NSString stringWithFormat:@"%d", (int)order+1];
    
//    [NSString stringWithFormat:@"%d", model.order]

    // 原价 有中文或者¥，有时候中划线显示不了，苹果的bug
    NSString *newPrice = [NSString stringWithFormat:@"¥%@", @"2111.00"];
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:newPrice attributes:attribtDic];
        self.cellOriginalPriceLabel.attributedText = attribtStr;
}

@end
